package com.sgcc.pda.hzaidl.impl;


import android.text.TextUtils;

import com.sgcc.pda.hw.ReturnResult;
import com.sgcc.pda.hzaidl.AIDLServiceImpl;
import com.sgcc.pda.hzaidl.IHZAidl;
import com.sgcc.pda.hzaidl.IHZAidlCallback;
import com.sgcc.pda.hzaidl.entity.HardWareCallback;
import com.sgcc.pda.hzaidl.entity.RequestBean;
import com.sgcc.pda.hzaidl.entity.ResponseBean;
import com.sgcc.pda.tools.JsonUtils;
import com.sgcc.pda.tools.LogPrintUtils;
import com.sgcc.pda.util.PlatConstant;
import com.sgcc.platforminterface.PlatfInterfManager;

import java.util.ArrayList;
import java.util.List;

/**
 * @创建者：suven on 2019/12/16 17:25
 * @描述：
 * @版本：1.0
 * @修改人：jianiuqi
 * @修改时间：2020/07/14
 * @修改说明：中台版调用认证后处理逻辑对结果处理有细化
 * @修改版本：1.0.1
 */
public class HardWareUtil {

    private static IHZAidl mHZAidl;

    private static AIDLServiceImpl getHardWareManager() {
        return AIDLServiceImpl.getInstance();
    }

    /**
     * 基础硬件服务，带返回值
     *
     * @param methodName 方法名
     */
    public static String requestBasicData(String methodName) {
        return requestBasicData(methodName, "");
    }

    /**
     * 基础硬件服务，带返回值
     *
     * @param methodName 方法名
     * @param paramJson  参数列表
     */
    public static String requestBasicData(String methodName, String paramJson) {
        RequestBean requestBean = getBasic(methodName, paramJson);
        String result = getHardWareManager().post(requestBean.toString(), null);
        ResponseBean responseBean = JsonUtils.toBean(result, ResponseBean.class);
        if (responseBean != null && responseBean.getRetCode() == 1) {
            return responseBean.getMsgJson();
        } else {
            return "";
        }
    }


    /**
     * 硬件服务方法，带返回值
     *
     * @param methodName 方法名
     */
    public static String requestHardwareDataString(String methodName) {
        return requestHardwareDataString(methodName, "");
    }

    /**
     * 硬件服务方法，带返回值
     *
     * @param methodName 方法名
     * @param paramJson  参数列表
     */
    public static String requestHardwareDataString(String methodName, String paramJson) {
        RequestBean requestBean = getHardware(methodName, paramJson);
        String result = getHardWareManager().post(requestBean.toString(), null);
        ResponseBean responseBean = JsonUtils.toBean(result, ResponseBean.class);
        if (responseBean != null && responseBean.getRetCode() == 1) {
            return responseBean.getMsgJson();
        } else {
            return "";
        }
    }

    /**
     * 硬件服务方法，带返回值
     *
     * @param methodName 方法名
     */
    public static ReturnResult requestHardwareData(String methodName) {
        return requestHardwareData(methodName, "");
    }

    public static ReturnResult requestHardwareData(String methodName, String paramJson) {
        return requestHardwareData(true, methodName, paramJson);
    }

    /**
     * @param methodName 方法名
     * @param paramJson  参数列表
     */
    public static ReturnResult requestHardwareData(boolean isCheckBluetooth, String methodName, String paramJson) {
        RequestBean requestBean = getHardware(methodName, paramJson);
        String result = isCheckBluetooth
                ? getHardWareManager().post(requestBean.toString(), null)
                : getHardWareManager().postBase(requestBean.toString(), null);
        LogPrintUtils.e("methodName: " + methodName + " \nrequest： " + requestBean.toString() + "\nresponse: " + result);
        ResponseBean responseBean = JsonUtils.toBean(result, ResponseBean.class);
        if (responseBean != null && responseBean.getRetCode() == 1) {
            return JsonUtils.toBean(responseBean.getMsgJson(), ReturnResult.class);
            // responseBean.retCode 为0时表示失败
        } else if (responseBean == null || responseBean.getRetCode() == 0) {
            ReturnResult returnResult = new ReturnResult();
            List<String> data = new ArrayList<>();
            returnResult.setCode(-1);
            if (responseBean != null) {
                data.add(responseBean.getMsgJson());
            }
            returnResult.setDatas(data);
            return returnResult;
        } else
            return null;
    }

    /**
     * 硬件服务方法，带回调返回值
     *
     * @param methodName 方法名
     * @param callback   回调
     */
    public static void requestHardwareData(String methodName, final HardWareCallback callback) {
        requestHardwareData(methodName, "", callback);
    }

    /**
     * @param isCheckBluetooth 是否需要判断蓝牙连接状态，只有背夹模式下需要，由于背夹模式下有可能不需要判断蓝牙连接状态
     * @param methodName       方法名
     * @param paramJson        参数列表
     */
    public static void requestHardwareData(boolean isCheckBluetooth, String methodName, String paramJson, final HardWareCallback callback) {
        RequestBean requestBean = getHardware(methodName, paramJson);
        IHZAidlCallback.Stub aidlCallback = new IHZAidlCallback.Stub() {
            @Override
            public void callback(String result) {
                try {
                    if (TextUtils.isEmpty(result)) {
                        callback.onReceiverData(null);
                        return;
                    }
                    ResponseBean responseBean = JsonUtils.toBean(result, ResponseBean.class);
                    if (responseBean != null) {
                        if (responseBean.getRetCode() == 1) {
                            ReturnResult returnResult = JsonUtils.toBean(responseBean.getMsgJson(), ReturnResult.class);
                            callback.onReceiverData(returnResult);
                        } else {
                            callback.onReceiverData(null);
                        }
                    } else {
                        callback.onReceiverData(null);
                    }
                } catch (Exception e) {
                    LogPrintUtils.d("requestHardwareData:", e);
                    callback.onReceiverData(null);
                }
            }
        };
        if (isCheckBluetooth) {
            getHardWareManager().post(requestBean.toString(), aidlCallback);
        } else {
            getHardWareManager().postBase(requestBean.toString(), aidlCallback);
        }
    }

    /**
     * @param isCheckBluetooth 是否需要判断蓝牙连接状态，只有背夹模式下需要，由于背夹模式下有可能不需要判断蓝牙连接状态
     * @param methodName       方法名
     * @param paramJson        参数列表
     * @param centerVersion    是否是中台版
     */
    public static void requestHardwareData(boolean isCheckBluetooth, String methodName, String paramJson, boolean centerVersion, final HardWareCallback callback) {
        if (!centerVersion) {
            requestHardwareData(isCheckBluetooth, methodName, paramJson, callback);
            return;
        }
        RequestBean requestBean = getHardware(methodName, paramJson);
        IHZAidlCallback.Stub aidlCallback = new IHZAidlCallback.Stub() {
            @Override
            public void callback(String result) {
                if (TextUtils.isEmpty(result)) {
                    callback.onReceiverData(new ReturnResult(-1, "返回值为空"));
                    return;
                }
                ResponseBean responseBean = JsonUtils.toBean(result, ResponseBean.class);
                if (responseBean != null) {
                    int retCode = responseBean.getRetCode();
                    String msgJson = responseBean.getMsgJson();
                    if (retCode == 1) {
                        // 成功为0
                        ReturnResult returnResult = JsonUtils.toBean(msgJson, ReturnResult.class);
                        //若转换后为null，返回自己填充的数据，不为空时直接返回
                        if (returnResult == null) {
                            ArrayList<String> list = new ArrayList<>();
                            list.add(msgJson);
                            returnResult = new ReturnResult(0, list);
                        }

                        callback.onReceiverData(returnResult);
                    } else if (retCode == 0) {
                        // 表示执行过程
                        callback.onReceiverData(new ReturnResult(-1, responseBean.getMsgJson()));
                    } else {
                        callback.onReceiverData(new ReturnResult(retCode, responseBean.getMsgJson()));
                    }
                } else {
                    callback.onReceiverData(new ReturnResult(-1, "返回值转换失败"));
                }
            }
        };
        getHardWareManager().postBase(requestBean.toString(), aidlCallback);
    }

    public static void requestHardwareData(String methodName, String paramJson, final HardWareCallback callback) {
        requestHardwareData(true, methodName, paramJson, callback);
    }

    /**
     * 原生调用方式
     *
     * @param methodName 方法名
     * @param paramJson
     * @param callback   结果回调
     */
    public static void requestHardwareData(String methodName, String paramJson, IHZAidlCallback.Stub callback) {
        RequestBean requestBean = getHardware(methodName, paramJson);
        getHardWareManager().post(requestBean.toString(), callback);
    }

    public static void requestHardwareData(boolean isCheckBluetooth, String methodName, String paramJson, IHZAidlCallback.Stub callback) {
        RequestBean requestBean = getHardware(methodName, paramJson);
        if (isCheckBluetooth) {
            getHardWareManager().post(requestBean.toString(), callback);
        } else {
            getHardWareManager().postBase(requestBean.toString(), callback);
        }

    }

    /**
     * 硬件方法组织请求参数
     *
     * @param methodName 方法名
     * @param paramJson  参数
     * @return 返回请求对像
     */
    private static RequestBean getHardware(String methodName, String paramJson) {
        return new RequestBean(PlatConstant.IWebCmd.HARDWARE, methodName, paramJson, PlatfInterfManager.getInstance().getPackageName(), "");
    }

    /**
     * 基础服务方法组织请求参数
     *
     * @param methodName 方法名
     * @param paramJson  参数
     * @return 返回请求对像
     */
    private static RequestBean getBasic(String methodName, String paramJson) {
        return new RequestBean(PlatConstant.IWebCmd.BASIC, methodName, paramJson, PlatfInterfManager.getInstance().getPackageName(), "");
    }
}
